﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.VisualStyles;
using DevExpress.XtraEditors;
using DevExpress.Utils;
using DevExpress.XtraBars.Docking2010.Views;
using DevExpress.XtraBars.Docking2010.Views.WindowsUI;
using DevExpress.XtraEditors;
using DevExpress.Utils.About;
using ECare.BUS;
using ECare.BUS.Data;
using ECare.Client.frms;
using ECare.Win.controls;



namespace ECare.Win
{
    public partial class frmMain : DevExpress.XtraEditors.XtraForm
    {
        public frmMain()
        {
            InitializeComponent();


            windowsUIView.ContentContainerActions.Add(new SetSkinAction("Metropolis", "White Theme"));
            windowsUIView.ContentContainerActions.Add(new SetSkinAction("MetroBlack", "Black Theme"));

            this.Load += frmMain_Load;

        }
        object current;
        void windowsUIView_QueryControl(object sender, QueryControlEventArgs e)
        {
            BaseModule module = e.Document.Tag is BaseModule ? (BaseModule)e.Document.Tag :
             Activator.CreateInstance(typeof(frmMain).Assembly.GetType(e.Document.ControlTypeName)) as BaseModule;
            module.InitModule(barManager1, windowsUIView);
            BaseTile tile = null;
            if (windowsUIView.Tiles.TryGetValue(e.Document, out tile))
            {
                TileItemFrame frame = tile.CurrentFrame;
                object data = current ?? ((frame != null) ? frame.Tag : null);
                module.ShowModule(data);
            }
            e.Document.Tag = module;
            e.Control = module;
        }

        void frmMain_Load(object sender, EventArgs e)
        {

            this.windowsUIView.Tiles.Clear();
            this.windowsUIView.Documents.Clear();
            this.mainGroup.Items.Clear();


            var frm = new frmLogin();
            if (frm.ShowDialog() == DialogResult.Cancel)
            {
                this.Close();
            }else
            {
                //分配当前的权限.
                ApplicationData.Permissions = RoleInfoHelper.GetRoleById(ApplicationData.CurrentUserInfo.RoleId).Permissions;
                //加载模块.
                this.LoadTiles();
                this.windowsUIView.QueryControl += windowsUIView_QueryControl;
                this.windowsUIView.TileClick += windowsUIView_TileClick;
            }}

        private void LoadTiles()
        {


            var modules = ModulesHelper.GetMenuItems();

            var rootmodules = modules.Where(a => a.ModuleParentId == -1);
            int i = 1;
            foreach (var module in rootmodules)
            {var elementText = new DevExpress.XtraEditors.TileItemElement { Text = module.ModuleName };
                elementText.TextAlignment = DevExpress.XtraEditors.TileItemContentAlignment.MiddleCenter;
                
                
                elementText.ImageAlignment = TileItemContentAlignment.MiddleCenter;

                elementText.Appearance.Normal.Font = new System.Drawing.Font("Tahoma", 12F);


                elementText.Image =
                    module.ModuleId == 4 ? global::ECare.Win.Properties.Resources.editdatasource_32x32 :
                    module.ModuleId == 4 ? global::ECare.Win.Properties.Resources.apply_32x32 :
                    module.ModuleId == 5 ? global::ECare.Win.Properties.Resources.workweekview_32x32 :
                    module.ModuleId == 8 ? global::ECare.Win.Properties.Resources.customization_32x32 :
                   global::ECare.Win.Properties.Resources.addnewdatasource_32x32;


                elementText.ImageToTextAlignment = TileControlImageToTextAlignment.Top;
                

                var item = new Tile(this.components);


                item.Name = "module_" + module.ModuleId; item.Group = module.groupName;
                item.Padding = new Padding(0);
                item.Properties.BackgroundImageAlignment = DevExpress.XtraEditors.TileItemContentAlignment.MiddleCenter;
                item.Properties.BackgroundImageScaleMode = TileItemImageScaleMode.ZoomOutside;
                //Create document
                item.Appearances.Normal.BackColor = module.BackColor;
                item.Appearances.Normal.BackColor2 = module.BackColor2;
                item.Appearances.Normal.BorderColor = module.BorderColor;

                item.Properties.ItemSize = module.Size == ItemSize.Large
    ? TileItemSize.Large
    : module.Size == ItemSize.Medinum
        ? TileItemSize.Medium
        : module.Size == ItemSize.Small
            ? TileItemSize.Small
            : module.Size == ItemSize.Wide ? TileItemSize.Wide : TileItemSize.Default;



                var document = new Document(this.components);
                document.Caption = module.ModuleName;
                document.ControlName = module.controlName;
                document.ControlTypeName = module.frmType;


                //   document.ControlName = "ucSettingsDocument";



                this.mainGroup.SetID(item, i);
                //  this.mainGroup.Properties.Margin = new System.Windows.Forms.Padding(40, 0, 40, 20);


                this.windowsUIView.Documents.Add(document as BaseDocument);
                this.windowsUIView.Tiles.Add(item as BaseTile);
                //  



                this.mainGroup.Items.Add(item);

                this.pageGroup.Items.Add(document);


                item.Document = document;
                item.Elements.Add(elementText);

                i++;
            }
        }


        void windowsUIView_TileClick(object sender, TileClickEventArgs e)
        {
            Tile tile = e.Tile as Tile;
            if (tile != null && tile.Document != null)
            {
                BaseModule module = tile.Document.Control as BaseModule;
                if (module != null)
                {
                    TileItemFrame frame = tile.CurrentFrame;
                    object data = (frame != null) ? frame.Tag : null;
                    module.ShowModule(data);
                }

                if (tile.ActivationTarget == page)
                {
                    page.Document = tile.Document;
                    page.Caption = tile.Elements[0].Text;
                }
            }
        }
    }
}